/**
 *
总结：
    一、两种方法进行缓存
        1.在每个vue页面创建之前，使用
        如：
         beforeCreate: function(){
            this.$nextTick(function () {
              nowRoute = this.$route;
              getCache(this);
              saveCache(this);
            });
        },
        2.对layui列表进行缓存，table列表缓存
         如：
        layuiCachedTable(this,params),params是layui.form.render(params)的参数
    二、三种方法取消缓存
        1.noCheckCache = true;能够让本次查询不进行缓存。
        2.flushCache(that);能够将本路由的缓存全部清除
        3.用post方法实现，在其后端加上不需要缓存的字段
        this.$post(url, {},this,'不需要缓存的字段')
    三、判断字段是否进行缓存了，用ifnotCached(that,"需要的字段")
 **/
/**
 * 通过cache保存起来 的layui的表格
 * @returns {string}
 */
var url = "";
// 为true时清除当前字段的缓存
export var layuiCachedTable = function (that,params,url="") {
    try{
        if (url==""){
            url = params.url;//缓存起来
        }
        if (components[that.$route.path]==undefined){
            components[that.$route.path]=[];
        }
        if (components[that.$route.path]['table']==undefined){
            components[that.$route.path]['table']=[];
        }
        if (components[that.$route.path]['table'][params.elem]==undefined){
            components[that.$route.path]['table'][params.elem]=[];
        }
        var t1 = components[that.$route.path]['table'][params.elem][currTablePage];//实现将所有的数据进行缓存到本地
        if((t1!=undefined&&t1!=null&&t1!=""&&t1!=[]) && !noCheckCache){
            let tempP = params;
            that.currentPage = currTablePage;
            tempP.page={curr:that.currentPage};
            tempP.url = burl + 'common/null-lay-data_';
            tempP.parseData = function(res){ //res 即为原始返回的数据
                try {
                    let te = components[that.$route.path]['table'][params.elem][currTablePage];
                    if (te!=undefined&&te.data[0]!=""){
                        return te;
                    }else{
                        components[that.$route.path]['table'][params.elem][currTablePage] = "";
                        params.url= url;
                        params.page={curr:currTablePage};
                        layuiCachedTable(that,params,url);
                    }
                }catch (e) {
                   consoleLog(e)
                }
            }
            if (params.url==undefined){
                noCheckCache = false;//不检查缓存
            }
            setTimeout(()=>{
                noCheckCache = false;//不检查缓存
            })
            layui.table.render(tempP);
        }else{
            params.parseData = function(res){ //res 即为原始返回的数据
                try {
                    let te = components[that.$route.path]['table'][params.elem][currTablePage];
                    if (te==undefined || te=="" || noCheckCache){
                        if (components[that.$route.path]==undefined){
                            components[that.$route.path]=[];
                        }
                        if (components[that.$route.path]['table']==undefined){
                            components[that.$route.path]['table']=[];
                        }
                        if (components[that.$route.path]['table'][params.elem]==undefined){
                            components[that.$route.path]['table'][params.elem]=[];
                        }
                        components[that.$route.path]['table'][params.elem][currTablePage] = res;
                    }else{
                        params.url= burl + 'common/null-lay-data_';
                        params.page={curr:currTablePage};
                        layuiCachedTable(that,params,url);
                    }
                }catch (e) {
                   consoleLog(e)
                }
            }
            params.url= url;
            if (params.url==undefined){
                noCheckCache = false;//不检查缓存
            }
            // setTimeout(()=>{
            //     noCheckCache = false;//不检查缓存
            // })
            layui.table.render(params);
        }
    }catch (e) {
       consoleLog(e)
    }
}
/**
 * 找到组件是否没有缓存
 * @param that
 * @returns {boolean}
 */
export var ifnotCached = function(that,datas=""){
    let path = that.$route.path;//循环渲染到的那个组件
    let id = that.$el==undefined?'':that.$el.id;//循环渲染到的那个组件
    if (datas!=null&&datas!=""){//判断某一个字段是否缓存
        if (components[path]!=undefined && components[path]!=[]) {
            let z = components[path][id];
            if (z!=undefined && z!=[]) {
                let l = eval('z.' + datas);
                if (l != "" && l!= undefined){
                    let t = Object.keys(l).length;
                    if (t==0||t==undefined){//字段没有缓存，就返回true,需要查询数据库
                        return true
                    } else{
                        return false
                    }
                }else{
                    return l == "" || l == undefined;
                }
            }else{
                return true;
            }
        }else{
            return true;
        }
    }else{
        if (components[path]!=undefined && components[path]!=[]) {
            return components[path][id]==""||components[path][id]==undefined;
        }else{
            return true;
        }
    }
}
/**
 * 清除缓存
 * @param that
 * @returns {boolean}
 */
export var flushCache = function(that,datas=""){
    let id = that.$el.id;//循环渲染到的那个组件
    var children = [];
    that.$off('hook:destroyed');
    that.$my_.xunhuan(children,that,function (c,t) {
        t.$off('hook:destroyed');//不要让其摧毁页面的时候又重新将数据刷新回去
    },function (t) {
        t.$off('hook:destroyed');//不要让其摧毁页面的时候又重新将数据刷新回去
    })
    if (datas!=null&&datas!=""){//判断某一个字段是否缓存
        if (components[nowRoute.path]!=undefined && components[nowRoute.path]!=[]) {
            let z = components[nowRoute.path][id];
            if (z!=undefined && z!=[]) {
                eval('z.' + datas+'=[]');
            }
        }
    }else{
        if (components[nowRoute.path]!=undefined && components[nowRoute.path]!=[]){
            return components[nowRoute.path] = [];
        }
    }
}
//以c_开头的变量不进行缓存
//取出（点击的vue的）数据，
export var getCache = function(that){//保存然后取出数据，可以实现本地存储
    let id = that.$el.id;//循环渲染到的那个组件
    if (nowVue!="" && nowRoute!=""){
        if (components[nowRoute.path]==undefined){
            components[nowRoute.path]=[];
        }
        var t = components[nowRoute.path][id];//实现将所有的数据进行缓存到本地
        if(t!=undefined&&t!=null&&t!=""&&t!=[]){
            let k = Object.keys(t)
            let ks = k.filter(function (item) {
                return item.substr(0,2)!="c_";
            })
            for (let item in ks){
                that.$set(that,ks[item],t[ks[item]])
            }
        }
    }
}
// 监听（现在的vue的）数据 保存
export var saveCache = function(that){
    let id = that.$el.id;//循环渲染到的那个组件
    that.$on('hook:destroyed',() => {//监听所有的摧毁页面之前的事件
        if (closedNavIndex!=-1){//点击了关闭，之前的关闭了，摧毁了
            if(navTabList[closedNavIndex]['show']){//关闭状态
                if (components[nowRoute.path]==undefined){
                    components[nowRoute.path]=[];
                }
                components[nowRoute.path][id] = that.$data;
            }else{
                if (components[nowRoute.path]==undefined){
                    components[nowRoute.path]=[];
                }
                components[nowRoute.path][id] = [];
                components[nowRoute.path]['table'] = []
            }
        }else{//点击了其他的，之前的关闭了，摧毁了
            if (components[nowRoute.path]==undefined){
                components[nowRoute.path]=[];
            }
            components[nowRoute.path][id] = that.$data;
        }
    });
}
//设置cookie
export var setCookie = function (cName,value,expiredays=null){
    var exdate=new Date();
    if (expiredays!=null){
        exdate.setTime(exdate.getTime()+expiredays*1000);
    }
    document.cookie = cName+ "=" +encodeURI(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
//读取cookie
export var getCookie = function (cName){
    if (document.cookie.length>0){
        var cStart=document.cookie.indexOf(cName + "=");
        if (cStart!=-1){
            cStart=cStart + cName.length+1;
            var cEnd=document.cookie.indexOf(";",cStart);
            if (cEnd==-1) cEnd=document.cookie.length;
            return decodeURI(document.cookie.substring(cStart,cEnd));
        }
    }
    return "";
}
//删除cookie
export function delCookie (name) {
    var exp = new Date();  exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null){
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}
//设置cookie
export var setLocalStorage = function (cName,value){
    window.localStorage.setItem(cName,JSON.stringify(value));
}
//读取cookie
export var getLocalStorage = function (cName){
    if (window.localStorage.getItem(cName) == "undefined"){
        return "";
    }else{
        let res = JSON.parse(window.localStorage.getItem(cName));
        let t = typeof res;
        if (t!="object"&&res!=""){
            res = JSON.parse(res);
        }
        return res;
    }
}
//删除cookie
export var removeLocalStorage = function (cName){
    localStorage.removeItem(cName);
}
//刷新本页
export var refresh = function (){
    window.location.reload();
}
export var refreshOnce = function (){
    if(location.href.indexOf('#reloaded')==-1){
        location.href=location.href+"#reloaded";
        location.reload();
    }
}
/**
 * 刷新所有的数据 为空
 * @param that
 */
export var flushData = function (datas) {
    if (typeof datas == "object"){
        for (let item in datas){
            if (typeof eval("datas."+item) == "object"){
                flushData(item)
            }
            vue.$set(datas,item,"")
        }
    }
}